<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use App\Models\Order;
use Illuminate\Support\Facades\DB;
use App\Models\UserCharge;
use App\Models\UserScore;
use App\Models\UserScoreStream;
/**
 *
 * @author Joker
 *        
 */
class NotifyController extends Controller
{
    /**
     * 异步通知
     */
    public function webNotify()
    {
        // 验证请求。
        if (! app('alipay.web')->verify()) {
            Log::notice('Alipay notify post data verification fail.', [
                'data' => Request::instance()->getContent()
            ]);
            return 'fail';
        }
    
        // 判断通知类型。
        switch (Input::get('trade_status')) {
            case 'TRADE_SUCCESS':
            case 'TRADE_FINISHED':
                $out_trade_no = Input::get('out_trade_no');
                $trade_no = Input::get('trade_no');
                $order_time = Input::get('gmt_payment');
                $order = Order::where('order_sn','=',$out_trade_no)->first();
                $order->payordersn = $trade_no;
                $order->gmt_payment = $order_time;
                $socre = $order->price * 10;
                $attributes = array(
                    'user_id'       => $order->user_id,
                    'money'         => $order->price,
                    'products'      => 0,
                    'gold'          => $socre,
                );
                $socre_new = array(
                    'score' =>  $socre,
                    'gold'  => 0,
                    'user_id' =>$order->user_id,
                );
                DB::beginTransaction();
                try{
                    $order->save();
                    UserCharge::firstOrCreate($attributes);
                    $userScore = UserScore::find($order->user_id);
                    if(empty($userScore))
                    {
                        UserScore::firstOrCreate($socre_new);
                    }
                    else 
                    {
                        $userScore->incrementAdd('score',$socre);
                    }
                    DB::commit();
                }catch (\PDOException $e){
                    Log::info(['支付通知入库异常'=>$e->getMessage()]);
                    DB::rollBack();
                }
                Log::debug('Alipay notify post data verification success.', [
                'out_trade_no' => Input::get('out_trade_no'),
                'trade_no' => Input::get('trade_no')
                ]);
                break;
        }
    
        return 'success';
    }
    
    /**
     * 同步通知
     */
    public function webReturn()
    {
        // 验证请求。
        if (! app('alipay.web')->verify()) {
            Log::notice('Alipay return query data verification fail.', [
                'data' => Request::getQueryString()
            ]);
            return redirect()->to(url('/member/recharge/fail'));
        }
    
        // 判断通知类型。
        switch (Input::get('trade_status')) {
            case 'TRADE_SUCCESS':
            case 'TRADE_FINISHED':
                $out_trade_no = Input::get('out_trade_no');
                $trade_no = Input::get('trade_no');
                $order_time = Input::get('gmt_payment');
                $order = Order::where('order_sn','=',$out_trade_no)->first();
                $order->payordersn = $trade_no;
                $order->gmt_payment = $order_time;
                $socre = $order->price * 10;
                $attributes = array(
                    'user_id'       => $order->user_id,
                    'money'         => $order->price,
                    'products'      => 0,
                    'gold'          => $socre,
                );
                $socre_new = array(
                    'score' =>  $socre,
                    'gold'  => 0,
                    'user_id' =>$order->user_id,
                );
                $socre_stream = array(
                    'user_id'       =>  $order->user_id,
                    'action'        =>  '积分充值',
                    'action_url'    =>  '/member/recharge',
                    'type'          => 0,
                    'scores'        => $socre,
                    'is_read'       => 0
                );
                DB::beginTransaction();
                try{
                    $order->save();
                    UserCharge::firstOrCreate($attributes);
                    $userScore = UserScore::find($order->user_id);
                    if(empty($userScore))
                    {
                        UserScore::firstOrCreate($socre_new);
                    }
                    else 
                    {
                        $userScore->incrementAdd('score',$socre);
                    }
                    UserScoreStream::create($socre_stream);
                    DB::commit();
                }catch (\PDOException $e){
                    Log::info(['支付通知入库异常'=>$e->getMessage()]);
                    DB::rollBack();
                }
                Log::debug('Alipay notify get data verification success.', [
                'out_trade_no' => $out_trade_no,
                'trade_no' => $trade_no
                ]);
                break;
        }
    
        return redirect()->to(url('/member/recharge/success'));
    }
}